package com.fable.ntzw_ga.controller;


import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fable.common.constants.CommonConstant;
import com.fable.common.utils.PageInfoUtils;
import com.fable.common.vo.ResultVO;
import com.fable.ntzw_ga.entity.SysRole;
import com.fable.ntzw_ga.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;
import java.util.Collection;
import java.util.Map;

/**
 * @author Created by gzh 2023-02-19 23:49:51
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/ntzw_ga/sys-role")
@PreAuthorize("hasRole('admin')")
public class SysRoleController {
    @Autowired
    ISysRoleService roleService;

    @ApiOperation(value = "角色列表")
    @PostMapping ("/list")
    public ResultVO getDataList(@RequestBody Map<String, Object> map) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getStatus,true).last(CommonConstant.LIMIT);
//        PageInfo<SysRole> guestPageInfo = PageInfoUtils.getPageInfo(Convert.toInt(map.get("pageNum")), Convert.toInt(map.get("pageSize")),roleService.list(wrapper));
        return ResultVO.okResult();
    }

    @ApiOperation(value = "添加角色")
    @PostMapping("/add")
    public ResultVO addRole(@RequestBody SysRole sysRole) {
        return ResultVO.okResult(roleService.save(sysRole));
    }

    @ApiOperation(value = "获取单个角色信息")
    @GetMapping("getOne/{id}")
    public ResultVO getOne(@PathVariable String id) {
        return ResultVO.okResult(roleService.getById(id));
    }

    @ApiOperation(value = "修改角色")
    @PutMapping("/update")
    public ResultVO updateRole(@RequestBody SysRole sysRole) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getId,sysRole.getId());
        return ResultVO.okResult(roleService.update(sysRole,wrapper));
    }

    @ApiOperation(value = "删除角色")
    @PostMapping("/del")
    public ResultVO addRole(@RequestBody @Valid Map map) {
        roleService.removeByIds((Collection<? extends Serializable>) map.get("ids"));
        return ResultVO.okResult();
    }
}
